/**
 * Copyright (c) 2006 RiseSoft Co.,Ltd  
 * $Header$
 */
package net.risesoft.risebi.collection.web.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.LazyDynaBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import net.risesoft.risebi.collection.model.ComboDefine;
import net.risesoft.risebi.collection.service.ComboDefineManager;
import net.risesoft.risebi.collection.web.BaseAction;
import net.risesoft.risebi.commons.util.GUID;
import net.risesoft.risebi.commons.util.JSONUtil;
import net.sf.json.JSONObject;

@Namespace("/admin/ComboDefineAction")
public class ComboDefineAction extends BaseAction{
	private static final long serialVersionUID = -6918988679677359450L;
	private static Log log = LogFactory.getLog(ComboDefineAction.class);
	
	private ComboDefineManager comboDefineManager;
	
	@SuppressWarnings("unchecked")
	@Action(value="list",
			results={@Result(name="success", location="/admin/comboDefine/list.jsp")}
	)
	public String list() throws Exception {
		try{
			List list = comboDefineManager.find("select distinct t.type from ComboDefine t");
			ArrayList newlist = new ArrayList();
			for(Object o : list){
				Map map = new HashMap();
				map.put("guid", (String)o);
				map.put("name", (String)o);
				newlist.add(map);
			}	
			this.json = JSONUtil.getJsonString(null, null, newlist);
		}catch(Exception e){
			this.json = "[]";
		}

		return SUCCESS;
	}
	
	@Action(value="createClassify")
	public String createClassify() throws Exception {
		String type = this.source.trim();
		LazyDynaBean ldb = new  LazyDynaBean();		
		try{
			String guid = new GUID().toString();
			ComboDefine c = new ComboDefine();
			c.setGuid(guid);
			c.setType(type);
			c.setCode("new code");
			c.setName("new name");
			c.setTabIndex(1d);
			comboDefineManager.saveObject(c);
			
			ldb.set("success", true);
			ldb.set("reason", "增加字典类别成功!");
			ldb.set("type", type);
			this.json = JSONObject.fromObject(ldb).toString();
		}catch(Exception e){
			ldb.set("success", false);
			ldb.set("reason", e.getMessage());
			this.json = JSONObject.fromObject(ldb).toString();
		}
		
		return AJAX;
	}
	
	@Action(value="save")
	public String save() throws Exception {
		LazyDynaBean ldb = new  LazyDynaBean();
		try{
			ComboDefine[] cs = (ComboDefine[])JSONUtil.stringToArray(this.json, ComboDefine.class);
			for (ComboDefine c : cs){
				String guid = c.getGuid();
				if(guid==null || guid.trim().length()==0 || guid.startsWith("NEW")){
					c.setGuid(new GUID().toString(32));
				}
				comboDefineManager.saveObject(c);				
			}
			
			ldb.set("success", true);
			ldb.set("reason", "保存字典条目成功!");
			this.json = JSONObject.fromObject(ldb).toString();
		}catch(Exception e){
			ldb.set("success", false);
			ldb.set("reason", e.getMessage());
			this.json = JSONObject.fromObject(ldb).toString();
			e.printStackTrace();
		}

		return AJAX;
	}
	
	@SuppressWarnings("unchecked")
	@Action(value="remove")
	public String remove() throws Exception {
		LazyDynaBean ldb = new  LazyDynaBean();
		try{
			comboDefineManager.removeObject(ComboDefine.class, this.node);
			
			ldb.set("success", true);
			ldb.set("reason", "删除成功!");
			this.json = JSONObject.fromObject(ldb).toString();
		}catch(Exception e){
			ldb.set("success", false);
			ldb.set("reason", e.getMessage());
			this.json = JSONObject.fromObject(ldb).toString();
		}
		return AJAX;
	}
	
	@Action(value="refreshJson")
	public String refreshJson() throws Exception {
		try{
			if("jsonItems".equalsIgnoreCase(this.whichJson)){				
				Map map = comboDefineManager.getPagedItemsByType(this.source,this.start,this.limit);				
				this.json = JSONUtil.getJsonString("include","count,list,guid,type,code,name,tabIndex",map);
			}
		}catch(Exception e){
			this.json = "[]";
			e.printStackTrace();
		} 

		return AJAX;
	}

	public ComboDefineManager getComboDefineManager() {
		return comboDefineManager;
	}

	public void setComboDefineManager(ComboDefineManager comboDefineManager) {
		this.comboDefineManager = comboDefineManager;
	}
	
}
 